package com.deyuanyun.pic.ctrl.validatefun;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.validator.vinterface.ValidateInterface;
import com.deyuanyun.pic.ctrl.annotation.DbTable;
import com.deyuanyun.pic.ctrl.annotation.DeviceAnnotation;
import com.deyuanyun.pic.ctrl.domain.*;
import com.deyuanyun.pic.ctrl.service.EQPTBaseService;
import com.deyuanyun.pic.settings.controller.dto.ValveRoomDTO;
import com.deyuanyun.pic.settings.service.pipe.ValveRoomService;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * 模块描述
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/9/19）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 *
 */
public class AutoDevice implements ValidateInterface {
    @Override
    public <T, E, C> boolean apply(T t, E e, C c) throws NoSuchFieldException {
        Object obj= c;
        DbTable table=obj.getClass().getAnnotation(DbTable.class);
        Field f=(Field) e;
        String val= (String) t;

        EQPTBaseService service= SpringAppliction.getBean(EQPTBaseService.class);
        Object device=service.queryObjectByCustomNumber(getDeviceType(table.excelName()),val);
        if(device == null)
        {
            return false;
        }
        try {
            String id=BeanUtils.getSimpleProperty(device,"id");
            try{
                String pipeLineId=BeanUtils.getSimpleProperty(device,"pipeLineId");
                String basePipeLineId=BeanUtils.getSimpleProperty(c,"pipeLineId");
                if(!pipeLineId.equals(basePipeLineId) )
                {
                    return false;
                }
            }catch (Exception e1)
            {
                System.out.print("没有管道属性");
            }


            DeviceAnnotation deviceAnnotation = f.getAnnotation(DeviceAnnotation.class);
            if( deviceAnnotation != null)
            {
                BeanUtils.setProperty(c,deviceAnnotation.name(),id);
            }else{
                BeanUtils.setProperty(c,f.getName(),id);
            }


            try{
                //添加测试桩验证
                /**
                 * 目前没有想到好点的处理方法，我在excel的测试桩编号后面添加设备的测试桩ID 并且使用 _$_分开
                 * 然后在验证测试桩的时候，将他们分开来比较
                 */
                Field testStationId= c.getClass().getDeclaredField("testStationId");
                if(testStationId != null)
                {
                    String deviceTestStationId=BeanUtils.getSimpleProperty(device,"testStationId");
                    String testStationNumber=BeanUtils.getSimpleProperty(c,"testStationId");
                    if(testStationNumber != null && !"".equals(testStationNumber) && deviceTestStationId != null)
                    {
                        BeanUtils.setProperty(c,"testStationId",testStationNumber+"_$_"+deviceTestStationId);
                    }
                }
            }catch (Exception e1)
            {
                System.out.println("没有测试桩");
            }






            try{
                //添加所属场站阀室验证
                /**
                 * 目前没有想到好点的处理方法，我在excel的测试桩编号后面添加设备的测试桩ID 并且使用 _$_分开
                 * 然后在验证测试桩的时候，将他们分开来比较
                 */
                Field fpipeValveroomId= c.getClass().getDeclaredField("pipeValveroomId");
                if(fpipeValveroomId != null)
                {
                    ValveRoomService valveRoomService = SpringAppliction.getBean(ValveRoomService.class);

                    String pipeValveroomId=BeanUtils.getSimpleProperty(device,"pipeValveroomId");

                    String pipeValveroom=BeanUtils.getSimpleProperty(c,"pipeValveroomId");
                    String pipeValveroomName=BeanUtils.getSimpleProperty(c,"pipeValveroomId");

                    //根据场站阀室名称得到场站阀室ID
                    ValveRoomDTO valveRoomDTO = valveRoomService.getValueRoomByName(pipeValveroom);
                    if(valveRoomDTO != null)
                    {
                        pipeValveroom = valveRoomDTO.getId();
                    }
                    if(pipeValveroom != null && !"".equals(pipeValveroom))
                    {
                        BeanUtils.setProperty(c,"pipeValveroomId",pipeValveroomId+"_$_"+pipeValveroom+"_$_"+pipeValveroomName);
                    }
                }
            }catch (Exception e1)
            {
                System.out.println("没有场站阀室");
            }
            return true;
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        }
        return false;
    }

    @Override
    public String getName() {
        return null;
    }



    public static  Class getDeviceType(String name)
    {
        if(name.indexOf("辅助阳极地床") >= 0 )
        {
            return EqptAnodesbed.class;
        }else if(name.indexOf("阴保电源") >= 0 ) {

            return EqptCppowerr.class;
        }
        else if(name.indexOf("长效参比电极") >= 0 ) {
            return EqptReference.class;
        }
        else if(name.indexOf("绝缘装置") >= 0 ) {
            return EqptInsulatedJoint.class;
        }
        else if(name.indexOf("防浪涌保护器") >= 0 ) {
            return EqptAntiSurge.class;
        }
        else if(name.indexOf("跨接线") >= 0 ) {
            return EqptCpJumper.class;
        }
        else if(name.indexOf("阴保测试桩") >= 0 ) {
            return EqptCptestStation.class;
        }
        else if(name.indexOf("牺牲阳极") >= 0 ) {
            return EqptSaalAnode.class;
        }
        else if(name.indexOf("排流装置") >= 0 ) {
            return EqptCurrentDrainage.class;
        }

        return null;
    }
}
